package cn.wp.ewallet.wallet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.wp.common.core.model.WpPage;
import cn.wp.common.core.util.BeanUtil;
import cn.wp.ewallet.common.model.dto.MoneyTypeInfoDto;
import cn.wp.ewallet.common.service.MoneyTypeInfoService;
import cn.wp.ewallet.wallet.dao.TransAccountLogDao;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import cn.wp.ewallet.wallet.extension.util.EwalletWalletUtil;
import cn.wp.ewallet.wallet.model.dto.TransAccountLogDto;
import cn.wp.ewallet.wallet.model.dto.TransAccountLogReqDto;
import cn.wp.ewallet.wallet.model.dto.TransAccountLogRespDto;
import cn.wp.ewallet.wallet.model.dto.UserWalletRelDto;
import cn.wp.ewallet.wallet.model.entity.TransAccountLogDo;
import cn.wp.ewallet.wallet.model.entity.TransAccountLogDoo;
import cn.wp.ewallet.wallet.model.entity.TransAccountLogReqDoo;
import cn.wp.ewallet.wallet.service.TransAccountLogService;
import cn.wp.ewallet.wallet.service.UserWalletRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web3j.utils.Convert;

import java.math.BigDecimal;
import java.util.List;

import static cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant.MONEY_TYPE_SHQC;

/**
 * @ClassName TransAccountLogServiceImpl
 * @Author ruixule
 * @Date 2020/1/8 9:52
 */
@Service
public class TransAccountLogServiceImpl  implements TransAccountLogService {


    @Autowired
    private TransAccountLogDao transAccountLogDao;

    @Autowired
    private UserWalletRelService userWalletRelService;

    @Autowired
    private MoneyTypeInfoService moneyTypeInfoService;

    /**
     * 通过钱包地址串查询  该钱包的转账记录
     * @return
     */
    @Override
    public WpPage<TransAccountLogRespDto> getTransAccountLogByWalAdd(WpPage wpPage) {
        UserWalletRelDto userWalletRelDto = userWalletRelService.getUserWalletRel();
        WpPage<TransAccountLogDo> wpPageList=transAccountLogDao.selectTransAccountLogByWalletAdd(userWalletRelDto.getWalletAddress(), MONEY_TYPE_SHQC, wpPage);
        //查询SHQC数据精度
        MoneyTypeInfoDto shqc = moneyTypeInfoService.getMoneyTypeInfoByMoneyTypeId(MONEY_TYPE_SHQC);
        BigDecimal shqcUnit = BigDecimal.valueOf(Long.valueOf(shqc.getMoneyTypeUnit()));

        return wpPageList.doConvertDto(TransAccountLogRespDto.class, new BeanUtil.Processor<TransAccountLogDo, TransAccountLogRespDto>() {
            @Override
            public void convert(TransAccountLogDo source, TransAccountLogRespDto target) {
                if (null != source.getTransAmount()){
                    target.setTransAmount(EwalletWalletUtil.toUpperWei(source.getTransAmount(), shqcUnit).toPlainString());
                }
                if (null != source.getServiceCharge()) {
                    target.setServiceCharge(EwalletWalletUtil.toUpperWei(source.getServiceCharge(), shqcUnit).toPlainString());
                }
                if (null != source.getTotalAmount()) {
                    target.setTotalAmount(EwalletWalletUtil.toUpperWei(source.getTotalAmount(), shqcUnit).toPlainString());
                }
                target.setTransTime(DateUtil.formatDateTime(source.getTransTime()));
            }
        });
    }

    @Override
    public WpPage<TransAccountLogDto> getALLRecharge(String address, WpPage page) {
        WpPage<TransAccountLogDo> wpPage = transAccountLogDao.selectALLRecharge(address, page);
        return wpPage.doConvertDto(TransAccountLogDto.class);
    }

    @Override
    public void saveTransAccountLog(TransAccountLogDto transAccountLogDto) {
        transAccountLogDao.insertTransAccountLogDo(BeanUtil.copyProperties(transAccountLogDto, TransAccountLogDo.class));
    }

    @Override
    public List<TransAccountLogDto> getPendingList() {
        return BeanUtil.listCopyProperties(transAccountLogDao.selectPendingList(), TransAccountLogDto.class);
    }

    @Override
    public void modifyTransAccountLog(TransAccountLogDto transAccountLogDto) {
        transAccountLogDao.updateTransAccountLog(BeanUtil.copyProperties(transAccountLogDto, TransAccountLogDo.class));
    }

    @Override
    public WpPage<TransAccountLogRespDto> getCollectLog(TransAccountLogReqDto transAccountLogReqDto, WpPage wpPage) {
        WpPage<TransAccountLogDoo> wpPageList = transAccountLogDao.getCollectLog(BeanUtil.copyProperties(transAccountLogReqDto, TransAccountLogReqDoo.class), wpPage);

        return wpPageList.doConvertDto(TransAccountLogRespDto.class, new BeanUtil.Processor<TransAccountLogDoo, TransAccountLogRespDto>() {
            @Override
            public void convert(TransAccountLogDoo source, TransAccountLogRespDto target) {

                if (null != source.getTransAmount()){
                    target.setTransAmount(EwalletWalletUtil.toUpperWei(source.getTransAmount(), new BigDecimal(source.getMoneyTypeUnit())).toPlainString());
                }
                if (null != source.getServiceCharge()) {
                    target.setServiceCharge(Convert.fromWei(source.getServiceCharge().toString(), Convert.Unit.ETHER).toPlainString());
                }
                if (null != source.getTotalAmount()) {
                    target.setTotalAmount(EwalletWalletUtil.toUpperWei(source.getTotalAmount(), new BigDecimal(source.getMoneyTypeUnit())).toPlainString());
                }

                target.setTransTime(DateUtil.formatDateTime(source.getTransTime()));
            }
        });
    }

    @Override
    public TransAccountLogRespDto getLogById(String logId) {

        return BeanUtil.copyProperties(transAccountLogDao.getLogById(logId), TransAccountLogRespDto.class, new BeanUtil.Processor<TransAccountLogDoo,TransAccountLogRespDto>() {
            @Override
            public void convert(TransAccountLogDoo source, TransAccountLogRespDto target) {
                if (null != source.getTransAmount()){
                    target.setTransAmount(EwalletWalletUtil.toUpperWei(source.getTransAmount(), new BigDecimal(source.getMoneyTypeUnit())).toPlainString());
                }

                if (null != source.getTotalAmount()){
                    target.setTotalAmount(EwalletWalletUtil.toUpperWei(source.getTotalAmount(), new BigDecimal(source.getMoneyTypeUnit())).toPlainString());
                }
                if (null != source.getServiceCharge()){
                    target.setServiceCharge(Convert.fromWei(source.getServiceCharge(), Convert.Unit.ETHER).toString());
                }

                target.setTransTime(DateUtil.formatDateTime(source.getTransTime()));
            }
        });
    }


    @Override
    public WpPage<TransAccountLogRespDto> getTransAccountLogsInSys(TransAccountLogReqDto transAccountLogReqDto, WpPage wpPage) {
        WpPage<TransAccountLogDoo> transAccountLogsInsys = transAccountLogDao.getTransAccountLogsInsys(transAccountLogReqDto,wpPage);
        MoneyTypeInfoDto moneyTypeInfoDto = moneyTypeInfoService.getMoneyTypeInfoByMoneyTypeId(EwalletWalletConstant.MONEY_TYPE_SHQC);
        return transAccountLogsInsys.doConvertDto(TransAccountLogRespDto.class, new BeanUtil.Processor<TransAccountLogDoo, TransAccountLogRespDto>() {
            @Override
            public void convert(TransAccountLogDoo source, TransAccountLogRespDto target) {
                target.setTransAmount(EwalletWalletUtil.toUpperWei(source.getTransAmount(), new BigDecimal(moneyTypeInfoDto.getMoneyTypeUnit())).toPlainString());
                target.setTotalAmount(EwalletWalletUtil.toUpperWei(source.getTotalAmount(), new BigDecimal(moneyTypeInfoDto.getMoneyTypeUnit())).toPlainString());
                target.setServiceCharge(EwalletWalletUtil.toUpperWei(source.getServiceCharge(), new BigDecimal(moneyTypeInfoDto.getMoneyTypeUnit())).toPlainString());
                target.setTransTime(DateUtil.formatDateTime(source.getTransTime()));
            }
        });
    }
}
